package com.zaoniao.enstudy.infrastructure.persistence;

import com.github.pagehelper.PageHelper;
import com.zaoniao.enstudy.domain.entity.VocabularyWords;
import com.zaoniao.enstudy.domain.entity.bo.VocabularyWordsBo;
import com.zaoniao.enstudy.domain.repository.UserVocabularyWordsRepository;
import com.zaoniao.enstudy.infracore.enums.YesOrNo;
import com.zaoniao.enstudy.infracore.web.PagedGridResult;
import com.zaoniao.enstudy.infrastructure.convert.UserVocabularyWordsCrt;
import com.zaoniao.enstudy.infrastructure.mapper.UserVocabularyWordsMapper;
import com.zaoniao.enstudy.infrastructure.po.UserVocabularyWordsPo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 词汇表单词明细仓储层业务实现类.
 *
 * @author llb666
 */
@Repository
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserVocabularyWordsRepositoryImpl implements UserVocabularyWordsRepository {

    private final UserVocabularyWordsMapper vocabularyWordsMapper;

    /**
     * 新增词汇单词明细.
     *
     * @param userVocabularyWords 词汇单词.
     */
    @Override
    public void insert(VocabularyWords userVocabularyWords) {
        UserVocabularyWordsPo vocabularyWordsPo = UserVocabularyWordsCrt.INSTANCE.do2po(userVocabularyWords);
        vocabularyWordsPo.setCreateUser(userVocabularyWords.getUserId());
        vocabularyWordsPo.setUpdateUser(userVocabularyWords.getUserId());
        vocabularyWordsPo.setStatus(YesOrNo.YES.type);
        vocabularyWordsMapper.insertSelective(vocabularyWordsPo);
    }

    /**
     * 更换词汇表.
     *
     * @param vocabularyWords 待更换的单词信息.
     */
    @Override
    public void updVocabularyWords(VocabularyWords vocabularyWords) {
        UserVocabularyWordsPo vocabularyWordsPo = UserVocabularyWordsCrt.INSTANCE.do2po(vocabularyWords);
        vocabularyWordsPo.setUpdateUser(vocabularyWords.getUserId());
        vocabularyWordsPo.setStatus(YesOrNo.YES.type);

        Example example = new Example(UserVocabularyWordsPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("word", vocabularyWords.getWord());

        vocabularyWordsMapper.updateByExampleSelective(vocabularyWordsPo, example);
    }

    /**
     * 取消收藏
     *
     * @param vocabularyWordsId -
     */
    @Override
    public void updStatus(Integer vocabularyWordsId) {
        UserVocabularyWordsPo vocabularyWordsPo = new UserVocabularyWordsPo();
        vocabularyWordsPo.setId(vocabularyWordsId);
        vocabularyWordsPo.setStatus(YesOrNo.NO.type);
        vocabularyWordsMapper.updateByPrimaryKeySelective(vocabularyWordsPo);
    }

    /**
     * 删除词汇表单名下所有单词（逻辑删除）
     *
     * @param vocabularyId 词汇表Id
     */
    @Override
    public void updStatusByVocabularyId(Integer vocabularyId) {
        UserVocabularyWordsPo vocabularyWordsPo = new UserVocabularyWordsPo();
        vocabularyWordsPo.setVocabularyId(vocabularyId);
        vocabularyWordsPo.setStatus(YesOrNo.NO.type);

        Example example = new Example(UserVocabularyWordsPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("vocabularyId", vocabularyId);
        vocabularyWordsMapper.updateByExampleSelective(vocabularyWordsPo, example);
    }

    /**
     * 根据词汇表Id查询词汇表所有收藏的单词明细分页列表.
     *
     * @param vocabularyId 词汇表Id
     * @param page         第几页
     * @param size         每页显示数量
     * @return 单词列表分页数据
     */
    @Override
    public PagedGridResult<VocabularyWordsBo> getVocabularyWordsPage(Integer vocabularyId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<VocabularyWordsBo> userVocabularyPos = vocabularyWordsMapper.selectVocabularyWords(vocabularyId);
        return PagedGridResult.setterPagedGrid(userVocabularyPos);
    }

    /**
     * 检查单词是否已存在待新增的词汇表.
     *
     * @param vocabularyWords 待新增词汇表的单词信息
     * @return -
     */
    @Override
    public boolean checkVocabularyWords(VocabularyWords vocabularyWords) {
        Example example = new Example(UserVocabularyWordsPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("vocabularyId", vocabularyWords.getVocabularyId());
        criteria.andEqualTo("word", vocabularyWords.getWord());
        int count = vocabularyWordsMapper.selectCountByExample(example);
        return count > 0;
    }

}
